﻿@model Bricks.Business.Models.Document.DocumentInfo

@{
    ViewBag.Title = "文档管理";
    Layout = "~/Views/Shared/_UserPage.cshtml";
}

<script type="text/javascript">
    $(function () {
        initTable();

        initModel();  //初始化模块
    });
    function initTable(queryData) {
        $('#documentDataGrid').datagrid({
            url: '/Document/GetAllDocumentList',
            //iconCls: 'icon-bricks',
            nowrap: true,
            autoRowHeight: false,
            striped: true,
            collapsible: true,
            pagination: true,
            pageSize: 20,
            rownumbers: true,
            //sortName: 'ID',    //根据某个字段给easyUI排序
            //sortOrder: 'asc',
            remoteSort: false,
            idField: 'ID',
            checkOnSelect: false,
            selectOnCheck: false,
            singleSelect: true,
            queryParams: queryData,  //异步查询的参数
            toolbar: '#documentToolBar',
            columns: [[
                {
                    title: '@Html.LabelFor(model => model.FileName)', field: 'FileName', width: 350, sortable: true,
                    formatter: function (value, row, index) {
                        //target = "_blank"可避免乱码问题
                        //return '<a target="_blank" href="' + row.FilePath + '">' + row.FileName + '</a>';
                        //return '<a href="javascript:void(0)" onclick="$.Download(\'' + row.FilePath + '\',\'' + row.FileName + '\')">' + row.FileName + '</a>';
                        return '<a target="_blank" href="/Document/Download?filePath=' + row.FilePath + '&fileName=' + row.FileName + '">' + row.FileName + '</a>';
                    }
                },
                //文件路径，点击可直接下载
                @*{
                    title: '@Html.LabelFor(model => model.FilePath)', field: 'FilePath', width: 300, sortable: true,
                    formatter: function (value, row, index) {
                        return '<a href="' + value + '">' + value + '</a>';
                    }
                },*@
                { title: '@Html.LabelFor(model => model.FileSize)', field: 'FileSize', width: 100, sortable: true },
                { title: '@Html.LabelFor(model => model.UploadUserID)', field: 'UploadUser', width: 100, sortable: true },
                //{ title: '@Html.LabelFor(model => model.AddDate)', field: 'AddDate', width: 130, sortable: true, formatter: function (value, rec) { if (value) { return dateConvert(value); } } }
                //{ title: '@Html.LabelFor(m => m.Tag)', field: 'Tag', width: 180, sortable: true },
                //{ title: '所属模块', field: 'ModelName', width: 100, sortable: true },
                //{ title: '所属菜单', field: 'MenuName', width: 100, sortable: true },
                { title: '模块-菜单', field: 'ModelAndMenuName', width: 180, sortable: true },
                { title: '@Html.LabelFor(model => model.UploadTime)', field: 'UploadTime', width: 130, sortable: true }
            ]],
            //双击弹出预览
            onDblClickRow: function (rowIndex, rowData) {
                Document.Preview();
            }
        });
    }

    //初始化模块列表
    function initModel() {
        //模块（取菜单中PID为NULL的记录）
        $("#model").combobox({
            url: "/MenuInfo/GetMenuForSearch",
            editable: false, width: 155, panelHeight: "auto",
            valueField: "ID",
            textField: "MenuName",
            onSelect: function (record) {
                initMenu(record.ID);
            }
        });
    }

    //初始化菜单列表（根据模块进行筛选）
    function initMenu(pid) {
        $("#menu").combobox({
            url: "/MenuInfo/GetMenuForSearch?pid=" + pid,
            editable: false, width: 155, panelHeight: "auto",
            valueField: "ID",
            textField: "MenuName"
        });
    }

    var Document = {
        //删除
        Delete: function () {
            var arr = $('#documentDataGrid').datagrid('getSelections');

            if (arr.length > 0) {
                $.messager.confirm('操作提示', "确认删除所选择的记录吗？", function (r) {
                    if (r) {
                        var ids = [];  //定义选中的ID集合
                        $.each(arr, function (index, item) {  //循环每条选中的记录
                            ids.push(item.ID);   //将ID加入到选中的ID集合中
                        });

                        //Remark By Luozx 2014-04-25：arr.join(",")获取的是对象数组，此处应该获取ID的集合
                        $.post("/Document/Delete?ids=" + ids.join(","), { query: arr.join(",") }, function (result) {
                            if (result.ResultType == 0) {  //0表示成功
                                $.EasyuiSlide(result.Message);
                                Document.Reload();
                            }
                            else {
                                $.EasyuiMsg(result.Message);
                            }
                        });
                    }
                });

            } else {
                $.EasyuiMsg("请选择一条数据");
            }
            return false;
        },

        //刷新
        Reload: function () {
            $("#documentDataGrid").datagrid("reload");
        },

        //清除条件
        ClearSearch: function () {
            $('#model').combobox('clear');
            $('#menu').combobox('clear');
            //$("#documentDataGrid").datagrid("load");

            Document.Search();
        },

        //查找
        Search: function () {
            var modelID = $('#model').combobox('getValue');
            //如果选择了模块
            if (modelID.length > 0) {
                var menuID = $('#menu').combobox('getValue');
                //如果选择了菜单
                if (menuID.length > 0) {
                    $("#documentDataGrid").datagrid("load", {
                        modelID: modelID,
                        menuID: menuID
                    });
                }
                    //如果没有选择菜单
                else {
                    $("#documentDataGrid").datagrid("load", {
                        modelID: modelID
                    });
                }
            }
                //如果没有选择菜单
            else {
                $("#documentDataGrid").datagrid("load", {
                    modelID: null,
                    menuID: null
                });
            }
        }
    }
</script>

<div class="easyui-layout" fit="true">
    <div data-options="region:'north'" style="height:40px;">
        <div class="frombox datagrid-body">
            <table class="datagrid-btable" border="0" cellspacing="0" cellpadding="0">
                <tr class="datagrid-row">
                    <td class="th">
                        <div class="datagrid-cell">模块（一级菜单）：</div>
                    </td>
                    <td class="td">
                        <div class="datagrid-cell">
                            <input id="model" name="Model" class="easyui-combobox" />
                            @*<input id="model" class="easyui-combobox" name="Model" data-options="valueField:'ID',textField:'MenuName',url:'/MenuInfo/GetMenuForSearch'" />*@
                        </div>
                    </td>
                    <td class="th">
                        <div class="datagrid-cell">菜单（二级菜单）：</div>
                    </td>
                    <td class="td">
                        <div class="datagrid-cell">
                            <input id="menu" name="Menu" class="easyui-combobox" />
                        </div>
                    </td>
                    <td class="td">
                        <div class="datagrid-cell">
                            <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Document.ClearSearch()' data-options='plain:true,iconCls:"icon-blue_lego"'>清除条件</a>
                            <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Document.Search()' data-options='plain:true,iconCls:"icon-blue_lego"'>查找</a>
                        </div>
                    </td>
                </tr>
            </table>
        </div>
    </div>

    <div data-options="region:'center'">
        <table id="documentDataGrid" fit="true"></table>
        <div id="documentToolBar">
            @*<a href='javascript:void(0)' class='easyui-linkbutton' onclick='Document.Delete()' data-options='plain:true,iconCls:"icon-bell"'>删除</a>*@
            <a href='javascript:void(0)' class='easyui-linkbutton' onclick='Document.Search()' data-options='plain:true,iconCls:"icon-blue_lego"'>刷新</a>
        </div>
    </div>
</div>
